<?php
/**
 * Created by PhpStorm.
 * User: WUHENG
 * Date: 2022/10/14
 * Time: 0:59
 */

namespace app\words\keyword;


class keywords
{
    public static function keyword($content){
        if(!$content){
            keywords::return_json(301, '请提交需要检测的内容');
        }

        if($res = keywords::check_content($content)){
            if(isset($_POST['method']) && $_POST['method'] == 1){
                //过滤敏感词
                $content = str_replace($res, '*', $content);
                keywords::return_json(201, '检测到敏感词：' . $res, $content);
            }
            keywords::return_json(201, '检测到敏感词：' . $res);
        }
        keywords::return_json(200, '没有检测到敏感词');




    }
    public static function check_content($content){
        $file = './static/word.txt';
        //上面这个就是词库文件路径
        $sensitive_array = array_map('rtrim', file($file));
        foreach ($sensitive_array as $key => $value) {
            if ($value == ''){
                continue;
            }
            if (mb_strpos($content, $value) != false){
                return $value;
                //return true;
            }
        }
        return false;
    }

    public static function return_json($code, $msg, $data = ''){
        exit(json_encode([
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ]));
    }
}